﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;

namespace iPhoneTrackerWin.Data
{
    public abstract class SQLiteDBManager<T> : AbstractSingletonDBManager<T> where T:new()
    {
        protected SQLiteDBManager(String table)
        {
            this.TableName = table;
        }

        /// <summary>
        /// Liefert alle Einträge aus der Datenbank
        /// </summary>
        /// <returns></returns>
        public virtual DataTable getAll()
        {
            return Database.DefaultDatabase.executeDataTable("Select * From " + TableName);
        }

        /// <summary>
        /// Lädt alle Daten von einem Eintrag
        /// </summary>
        /// <param name="BoxID"></param>
        /// <returns></returns>
        public virtual DataTable getData(int ID)
        {
            Dictionary<String, object> d = new Dictionary<string, object>();
            d.Add("ID", ID);
            return Database.DefaultDatabase.executeDataTable("Select * From " + TableName + " Where ID=@ID", d);
        }

        /// <summary>
        /// Erzeugt einen neuen Eintrag
        /// </summary>
        /// <param name="data"></param>
        public virtual int create(Dictionary<String, object> data)
        {
            int newID = Convert.ToInt32(Database.DefaultDatabase.executeGetScalar("Insert Into " + TableName + " " + Database.DefaultDatabase.prepareInsertValues(data) + " Select @@Identity", data));
            return newID;
        }

        /// <summary>
        /// Bearbeitet einen Eintrag
        /// </summary>
        /// <param name="data"></param>
        public virtual void edit(Dictionary<String, object> data)
        {
            Database db = Database.DefaultDatabase;
            db.executeNonQuery("Update " + TableName + " Set " + db.prepareUpdateValues(data) + " Where ID=@ID", data);
        }

        /// <summary>
        /// Löscht einen Eintrag aus der Datenbank (persistent)
        /// </summary>
        /// <param name="BoxID"></param>
        public virtual void delete(int ID)
        {
            Dictionary<String, object> d = new Dictionary<string, object>();
            d.Add("ID", ID);
            Database.DefaultDatabase.executeNonQuery("Delete From " + TableName + " Where ID=@ID", d);
        }
    }
}
